package vip.xiaonuo.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.modular.entity.Result;
import vip.xiaonuo.modular.enums.ResultExceptionEnum;
import vip.xiaonuo.modular.mapper.ResultMapper;
import vip.xiaonuo.modular.param.ResultParam;
import vip.xiaonuo.modular.service.ResultService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 考生成绩表service接口实现类
 *
 * @author 春非昔日春
 * @date 2023-03-08 16:33:52
 */
@Service
public class ResultServiceImpl extends ServiceImpl<ResultMapper, Result> implements ResultService {

    @Override
    public PageResult<Result> page(ResultParam resultParam) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(resultParam)) {
            // 根据准考证id 查询
            if (ObjectUtil.isNotEmpty(resultParam.getUserId())) {
                queryWrapper.lambda().eq(Result::getUserId, resultParam.getUserId());
            }
            // 根据用户名称 查询
            if (ObjectUtil.isNotEmpty(resultParam.getUserName())) {
                queryWrapper.lambda().like(Result::getUserName, resultParam.getUserName());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Result> list(ResultParam resultParam) {
        return this.list();
    }

    @Override
    public void add(ResultParam resultParam) {
        Result result = new Result();
        BeanUtil.copyProperties(resultParam, result);
        this.save(result);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<ResultParam> resultParamList) {
        resultParamList.forEach(resultParam -> {
        Result result = this.queryResult(resultParam);
            this.removeById(result.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(ResultParam resultParam) {
        Result result = this.queryResult(resultParam);
        BeanUtil.copyProperties(resultParam, result);
        this.updateById(result);
    }

    @Override
    public Result detail(ResultParam resultParam) {
        return this.queryResult(resultParam);
    }

    /**
     * 获取考生成绩表
     *
     * @author 春非昔日春
     * @date 2023-03-08 16:33:52
     */
    private Result queryResult(ResultParam resultParam) {
        Result result = this.getById(resultParam.getId());
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException(ResultExceptionEnum.NOT_EXIST);
        }
        return result;
    }
}
